<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
	public Map getMap(List<Map> result , Map<String,String> deptMap){
		//处理结合数据
		for(Map map : result){

			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("name",deptMap.get(ksdm));

		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		return resMap;
	}
	
	// 将map按照时间字段值降序
	class MapComparator implements Comparator<Map> {
		@Override
		public int compare(Map map1, Map map2) {
			double time1 = 0;
			double time2 = 0;
			try {
				time1 = Double.parseDouble(Tools.getDesc(map1.get("xmje").toString().split("%")[0]));
				time2 = Double.parseDouble(Tools.getDesc(map2.get("xmje").toString().split("%")[0]));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return (time1 > time2) ? -1 : (time1 == time2) ? 0 : 1;
		}
	}
%>
<%
	//查询门诊 药占比
	String yydm =  input.getDesc("yydm","02");//医院代码
	String ksdm = input.getDesc("ksdm","-1"); //传入科室代码代表该科室主任
	String date = input.getDesc("start_date","2017-01-02");//开始时间
	String endDa = input.getDesc("end_date","2017-01-02");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";

	String sqlWhere = " and js.sfrq > '"+ startDate + "' and js.sfrq < '" + endDate+"' " ;
	if(!yydm.equals("-1"))
	sqlWhere += " and js.ksdm in (select new map (id as id) from his_dept where yydm ='"+yydm+"') " ;
	if(!"-1".equals(ksdm)){
		sqlWhere += " and js.ksdm in(" + ksdm + ")";
	}
	
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept ");
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	//门诊收入
	
	List<Map> feeSumlist = QBuilder.db("basedb").queryHql("select new map(sum(m.xmje) as xmje) from SF_BRJSMXK m , SF_BRJSK js where m.jssjh = js.sjh and js.jlzt in(0,2) and js.ybjszt=2  " + sqlWhere+ "  ");
	
	
	double money = 0;
	//取出总金额
	for(Map map : feeSumlist ){
		double moneyTemp = map.get("xmje") == null ? 0 : Double.parseDouble(map.get("xmje").toString());
		money += moneyTemp;
	}
	//System.out.println(money);
	//科室总收入集合
	List<Map> feelist2 = QBuilder.db("basedb").queryHql("select new map(sum(m.xmje) as xmje , js.ksdm as ksdm ) from SF_BRJSMXK m , SF_BRJSK js where m.jssjh = js.sjh and js.jlzt in(0,2) and js.ybjszt=2    " + sqlWhere+ "  group by js.ksdm order by sum(m.xmje) desc ");
	//System.out.println(feelist2);
	//药品占比
	List<Map> feelist = QBuilder.db("basedb").queryHql("select new map(sum(m.xmje) as xmje , js.ksdm as ksdm ) from SF_BRJSMXK m , SF_BRJSK js where m.jssjh = js.sjh and js.jlzt in(0,2) and js.ybjszt=2  and m.dxmdm in('02','03','01') " + sqlWhere+ "  group by js.ksdm order by sum(m.xmje) desc ");

	DecimalFormat df = new DecimalFormat("######0.00"); 
	//计算药占比
	double moneyyy = 0;
	int i = 0 ; 
	for(Map map : feelist ){
		double moneyTemp = map.get("xmje") == null ? 0 : Double.parseDouble(map.get("xmje").toString());
		moneyyy += moneyTemp;
		
		//计算科室药占比
		Map map2 = feelist2.get(i);
		i++;
		double moneyDept = map2.get("xmje") == null ? 1 : Double.parseDouble(map2.get("xmje").toString());
		map.put("xmje",df.format(moneyTemp ==0 ? 0 : moneyTemp / moneyDept *100) +"%");
	}
	//System.out.println(moneyyy);
	double ratio = moneyyy ==0 ? 0 : moneyyy / money *100;
	
		Collections.sort(feelist, new MapComparator());
	
	Map<String,Object> ma1 = new HashMap<String,Object>();
	Map<String,Object> ma = new HashMap<String,Object>();

	ma.put("sumCount",df.format(ratio));
	ma1.put("data",ma);
	
	ma1.put("deptList",getMap(feelist , deptMap));
	Message msg = new Message();

	msg.put("errcode",0);
	msg.put("chargeRatio",ma1);
	out.println(msg);
%>